package com.treasure.mall.biz.vo.product;

import com.treasure.mall.biz.entity.product.ProductPO;
import com.treasure.mall.zbq.common.StringUtilsExt;
import com.treasure.mall.zbq.common.ienum.base.CodeDescCommonEnum;
import com.treasure.mall.zbq.common.ienum.base.CodeDescEnumHelper;
import com.treasure.mall.zbq.common.ienum.product.ProductEnums;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wm
 */
@Data
@NoArgsConstructor
public class ProductApiDetailVo extends ProductApiVo {
    /**
     * 详情
     */
    private String detailContent;
    /**
     * 图片列表
     */
    private List<String> picList;
    /**
     * 类目名称
     */
    private List<String> catNames;

    /**
     * 服务标签名
     */
    private List<String> serviceTagNames;
    /**
     * 商品标签名
     */
    private List<String> productTagNames;


    /**
     * 支持7天无理由退换货
     */
    private Boolean enable7DaysReturnedPurchase;
    /**
     * 全国包邮
     */
    private Boolean enableNationwidePostingFree;

    public ProductApiDetailVo(ProductPO productPO) {
        super(productPO);
        this.detailContent = productPO.getDetailContent();
        this.enable7DaysReturnedPurchase = getServiceTags().contains(ProductEnums.ServiceTag.SEVEN_DAYS_NO_REASON_TO_REFUND.getCode().toString());
        this.enableNationwidePostingFree = getServiceTags().contains(ProductEnums.ServiceTag.FREE_SHIPPING_NATIONALLY.getCode().toString());
        this.productTagNames = getTagNames(getProductTags(), ProductEnums.ProductTag.class);
        this.serviceTagNames = getTagNames(getServiceTags(), ProductEnums.ServiceTag.class);
    }

    private List<String> getTagNames(String tags, Class<? extends CodeDescCommonEnum> cls) {
        if (StringUtils.isEmpty(tags)) {
            return new ArrayList<>();
        }
        String[] arr = StringUtilsExt.trim(tags, ',').split(",");
        List<String> nameList = new ArrayList<>();
        for (String s : arr) {
            if (StringUtils.isEmpty(s)) {
                continue;
            }
            nameList.add(CodeDescEnumHelper.getEnumDescByCode(cls, new Integer(s)).toString());
        }
        return nameList;
    }
}